home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-07-18 | 6.1 KB | 209 lines |
- // ServletGate - a program that runs Servlets
- //
- // Copyright (c) 1996 Sun Microsystems, Inc. All Rights reserved
- // Permission to use, copy, modify, and distribute this software
- // and its documentation for NON-COMMERCIAL purposes and without
- // fee is hereby granted provided that this copyright notice
- // appears in all copies. Please refer to the file copyright.html
- // for further important copyright and licensing information.
- //
- // SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
- // THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
- // TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
- // PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
- // ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
- // DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
-
- package sun.servlet.apache;
-
- import java.io.*;
- import java.util.*;
- import javax.servlet.*;
- import javax.servlet.http.*;
-
- /** A program that runs Servlets.
- ** <P>
- ** This is a web program in java, but instead of actually serving
- ** content it instead turns the web requests into Jeeves-compatible
- ** Servlet requests, and then runs the Servlets.
- */
-
- public abstract class ServletGate implements ServletContext {
-
- protected PrintStream logStream;
- protected Properties servletProperties;
-
- /** Constructor, specified log stream.
- */
- public ServletGate( PrintStream logStream ) {
- this.logStream = logStream;
- servletProperties = new Properties();
- }
-
- /** Constructor, specified log file.
- */
- public ServletGate( File logFile ) throws IOException {
- this( new PrintStream( new FileOutputStream( logFile ) ) );
- }
-
- /** Constructor, default log stream.
- */
- public ServletGate() {
- this( System.err );
- }
-
- /**
- * Load servlet properties from servlet prop file
- */
- protected void loadServletProps(String servletPropFile) {
- File spf = new File(servletPropFile);
- if (spf.exists() && spf.canRead()) {
- try {
- servletProperties.load(new FileInputStream(spf));
- } catch (IOException ioe) {
- log("Could not load servlet properites file");
- ioe.printStackTrace(logStream);
- }
-
- }
- }
-
-
- // Methods from ServletContext.
-
- protected Hashtable servlets = new Hashtable();
-
- /** Gets a servlet by name.
- ** @param name the servlet name
- ** @return null if the servlet does not exist
- */
- public Servlet getServlet( String name ) {
- return (Servlet) servlets.get( name );
- }
-
- /** Enumerates the servlets in this context (server). Only servlets that
- ** are accesible will be returned. This enumeration always includes the
- ** servlet itself.
- */
- public Enumeration getServlets() {
- return servlets.elements();
- }
-
- /** Destroys all currently-loaded servlets.
- */
- public void destroyAllServlets() {
- Enumeration en = servlets.elements();
- while ( en.hasMoreElements() ) {
- Servlet servlet = (Servlet) en.nextElement();
- servlet.destroy();
- }
- servlets.clear();
- }
-
- /** Write information to the servlet log.
- ** @param message the message to log
- */
- public void log( String message ) {
- Date date = new Date( System.currentTimeMillis() );
- logStream.println( "[" + date.toString() + "] " + message );
- }
-
- /** Write information to the servlet log.
- * @param servlet the servlet to log information about
- * @param message the message to log
- */
- public void log( Servlet servlet, String message ) {
- log( servlet.toString() + ": " + message );
- }
-
- /** Internal method to write exceptions to log file.
- * @param Throwable to log the stacktrace of.
- */
- void log( Throwable t) {
- t.printStackTrace(logStream);
- }
-
- /** Applies alias rules to the specified virtual path and returns the
- * corresponding real path. It returns null if the translation
- * cannot be performed.
- * @param path the path to be translated
- */
- public String getRealPath( String path ) {
- // No mapping.
- return path;
- }
-
- /** Returns the MIME type of the specified file.
- * @param file file name whose MIME type is required
- */
- public String getMimeType( String file ) {
- int lastDot = file.lastIndexOf( '.' );
- int lastSep = file.lastIndexOf( File.separatorChar );
- if (lastDot == -1 || (lastSep != -1 && lastDot < lastSep))
- return "text/plain";
- String extension = file.substring( lastDot + 1 );
- if ( extension.equals( "html" ) || extension.equals( "htm" ) )
- return "text/html";
- if ( extension.equals( "gif" ) )
- return "image/gif";
- if ( extension.equals( "jpg" ) || extension.equals( "jpeg" ) )
- return "image/jpeg";
- if ( extension.equals( "au" ) )
- return "audio/basic";
- if ( extension.equals( "ra" ) || extension.equals( "ram" ) )
- return "audio/x-pn-realaudio";
- if ( extension.equals( "wav" ) )
- return "audio/x-wav";
- if ( extension.equals( "mpg" ) || extension.equals( "mpeg" ) )
- return "video/mpeg";
- if ( extension.equals( "qt" ) || extension.equals( "mov" ) )
- return "video/quicktime";
- if ( extension.equals( "class" ) )
- return "application/octet-stream";
- if ( extension.equals( "ps" ) )
- return "application/postscript";
- if ( extension.equals( "wrl" ) )
- return "x-world/x-vrml";
- return "text/plain";
- }
-
- /** Returns the name and version of the web server under which the servlet
- * is running.
- * Same as the CGI variable SERVER_SOFTWARE.
- */
- public String getServerInfo() {
- return serverName() + " " + serverVersion();
- }
-
- /**
- * The name of this software.
- */
- public abstract String serverName();
-
- /**
- * The version of this software.
- */
- public abstract String serverVersion();
-
- /**
- * The URL for this software.
- */
- public abstract String serverUrl();
-
- /**
- * Writes an address for this software.
- */
- public void writeAddress( OutputStream o ) {
- PrintStream p = new PrintStream( o );
- p.println("<ADDRESS><A HREF=\"" + serverUrl() + "\">" +
- serverName() + " " + serverVersion() + "</A></ADDRESS>" );
- }
-
- /**
- * Returns a server defined attribute. Not currently implemented.
- */
- public Object getAttribute( String name ) {
- return null;
- }
- }
-